
//
//  Extenison+NSDate.swift
//  WLWeibo
//
//  Created by Devil on 16/8/15.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

private let df = NSDateFormatter()

let calender = NSCalendar.currentCalendar()

extension NSDate{

    class func getSinaDate(createAt: String) -> NSDate{
    
        df.dateFormat = "EEE MMM dd HH:mm:ss z yyyy"
        
        df.locale = NSLocale(localeIdentifier: "en_US")
        
        let sinaDate = df.dateFromString(createAt)!
        
        return sinaDate
        
    
    }
    
    var sinaDateDescription: String{
        
        
        let isThisYear = sinaIsThisYear(self)
        if isThisYear {
            let calender = NSCalendar.currentCalendar()
            
            if calender.isDateInToday(self) {
                let S = Int(NSDate().timeIntervalSinceDate(self))
                printLog(S)
                
                if S < 60  {
                    return "刚刚"
                }else if S >= 60 && S < 3600{
                    
                    return "\(S/60)分钟前"
                    
                }else{
                    
                    return "\(S/3600)小时前"
                    
                }
            }else if calender.isDateInYesterday(self){
                df.dateFormat = "昨天 HH:mm"
            }else{
                
                df.dateFormat = "MM月dd日 HH:mm"
            }
        }else{
            
            df.dateFormat = "yyyy年MM月dd日 HH:mm"
            
        }
        return df.stringFromDate(self)
        
    }
    
    func sinaIsThisYear(sinaDate: NSDate) -> Bool{
        let df = NSDateFormatter()
        df.dateFormat = "yyyy"
        df.locale = NSLocale(localeIdentifier: "en_US")
        let sinaTimeStr = df.stringFromDate(sinaDate)
        let currentTimeStr = df.stringFromDate(NSDate())
        return sinaTimeStr == currentTimeStr
    }

}

